const db = require("../dbs/db");
const jwt = require("jsonwebtoken")

class Ad_Controller {
    /**
     * 创建
     * @param ctx
     * @returns {Promise.<void>}
     */
    static async create(ctx, next) {
        const token = String(ctx.request.headers.authorization || '').split(' ').pop()
        if (!token) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        const { id } = jwt.verify(token, 'glory-of-kings')
        if (!id) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        ctx.req.user = await db.AdminUser.findOne({
            where: { id: id }
        })
        if (!ctx.req.user) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        await next()
        //接收客服端
        let req = ctx.request.body;
        if (req.name) {
            try {
                await db.Ad.create({
                    name: req.name,
                });
                const data = await db.Ad.findOne({
                    where: { name: req.name }
                })
                for (let i = 0; i < req.aditems.length; i++) {
                    await db.Aditem.create({
                        image: req.aditems[i].image,
                        url: req.aditems[i].url,
                        adId: data.id
                    });
                }
                ctx.response.status = 200;
                ctx.body = {
                    code: 200,
                    msg: '创建成功',
                    data
                }
            } catch (err) {
                ctx.response.status = 412;
                ctx.body = {
                    code: 412,
                    msg: '创建失败',
                    data: err
                }
            }
        } else {
            ctx.response.status = 416;
            ctx.body = {
                code: 200,
                msg: '参数不齐全'
            }
        }
    }

    /**
     * 获取文章详情
     * @param ctx
     * @returns {Promise.<void>}
     */
    static async detail(ctx, next) {
        const token = String(ctx.request.headers.authorization || '').split(' ').pop()
        if (!token) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        const { id } = jwt.verify(token, 'glory-of-kings')
        if (!id) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        ctx.req.user = await db.AdminUser.findOne({
            where: { id: id }
        })
        if (!ctx.req.user) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        await next()
        let cid = ctx.params.id;
        if (cid) {
            try {
                // 查询详情模型
                let data = await db.Ad.findOne({
                    where: { id: cid }, include: [{
                        model: db.Aditem
                    }]
                })
                ctx.response.status = 200;
                ctx.body = {
                    code: 200,
                    msg: '查询成功',
                    data
                }
            } catch (err) {
                ctx.response.status = 412;
                ctx.body = {
                    code: 412,
                    msg: '查询失败',
                    data
                }
            }
        } else {
            ctx.response.status = 416;
            ctx.body = {
                code: 416,
                msg: 'ID必须传'
            }
        }
    }

    static async update(ctx, next) {
        const token = String(ctx.request.headers.authorization || '').split(' ').pop()
        if (!token) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        const { id } = jwt.verify(token, 'glory-of-kings')
        if (!id) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        ctx.req.user = await db.AdminUser.findOne({
            where: { id: id }
        })
        if (!ctx.req.user) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        await next()
        let req = ctx.request.body;
        let cid = ctx.params.id;
        if (req.name && cid) {
            try {
                await db.Ad.update({ name: req.name }, { where: { id: cid } })
                let data = await db.Ad.findOne({
                    where: { name: req.name }
                })
                await db.Aditem.destroy({
                    where: {
                        adId: cid
                    }
                })
                for (let i = 0; i < req.aditems.length; i++) {
                    await db.Aditem.create({
                        image: req.aditems[i].image,
                        url: req.aditems[i].url,
                        adId: cid
                    });
                }
                ctx.response.status = 200;
                ctx.body = {
                    code: 200,
                    msg: '更新成功',
                    data
                }
            } catch (err) {
                ctx.response.status = 412;
                ctx.body = {
                    code: 412,
                    msg: '更新失败',
                }
            }
        } else {
            ctx.response.status = 416;
            ctx.body = {
                code: 416,
                msg: '缺少ID值或title值'
            }
        }
    }

    static async delete(ctx, next) {
        const token = String(ctx.request.headers.authorization || '').split(' ').pop()
        if (!token) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        const { id } = jwt.verify(token, 'glory-of-kings')
        if (!id) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        ctx.req.user = await db.AdminUser.findOne({
            where: { id: id }
        })
        if (!ctx.req.user) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        await next()
        let cid = ctx.params.id;
        try {
            let data = await db.Ad.findOne({
                where: { id: cid }
            })
            await db.Aditem.destroy({
                where: {
                    adId: cid
                }
            })
            await db.Ad.destroy({
                where: {
                    id: cid
                }
            })
            ctx.response.status = 200;
            ctx.body = {
                code: 200,
                msg: `删除${data.title}成功`,
            }
        } catch (err) {
            ctx.response.status = 412;
            ctx.body = {
                code: 412,
                msg: '删除失败',
            }
        }
    }

    static async list(ctx) {
        let data = await db.Ad.findAll(
            {
                include: [{
                    model: db.Aditem,
                    attributes: ['image', 'url']
                }],
            }
        )
        ctx.response.status = 200;
        ctx.body = {
            code: 200,
            msg: '查询成功',
            data

        }
    }



}

module.exports = Ad_Controller;